<template>
  <div class="mod-config">
    <el-form :inline="true" :model="dataForm" ref="dataForm"  @keyup.enter.native="getDataList()">
      <el-form-item>
        <el-input v-model="dataForm.certificate_id" placeholder="船员姓名" clearable></el-input>
      </el-form-item>
      <el-form-item prop="certificateName">
        <el-input v-model="dataForm.certificateName" placeholder="证书名称" clearable></el-input>
      </el-form-item>
      <el-form-item prop="certificateState">
        <el-select v-model="dataForm.certificateState" placeholder="状态">
          <el-option
            v-for="item in optionsCertificateState"
            :key="item.value"
            :label="item.label"
            :value="item.value"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button @click="getDataList()">查询</el-button>
        <el-button @click="resetForm('dataForm')">重置</el-button>
        <!-- <el-button v-if="isAuth('generator:certificatemanagement:save')" type="primary" @click="addOrUpdateHandle()">新增</el-button> -->
        <!-- <el-button
          v-if="isAuth('generator:certificatemanagement:delete')"
          type="danger"
          @click="deleteHandle()"
          :disabled="dataListSelections.length <= 0"
        >批量删除</el-button>-->
      </el-form-item>
    </el-form>
    <el-table
      :data="dataList"
      border
      v-loading="dataListLoading"
      @selection-change="selectionChangeHandle"
      :row-class-name="tableRowClassName"
      style="width: 100%;"
    >
      <!-- <el-table-column type="selection" header-align="center" align="center" width="50"></el-table-column> -->
      <el-table-column prop="crewName" header-align="center" align="center" label="船员姓名"></el-table-column>
      <el-table-column prop="crewPost" header-align="center" align="center" label="船员职务"></el-table-column>
      <el-table-column prop="certificateName" header-align="center" align="center" label="证书名称"></el-table-column>
      <el-table-column prop="certificateNumber" header-align="center" align="center" label="证书号码"></el-table-column>
      <el-table-column prop="certificateTime" header-align="center" align="center" label="签发时间"></el-table-column>
      <el-table-column prop="expirationDate" header-align="center" align="center" label="到期时间"></el-table-column>
      <el-table-column prop="overDate" header-align="center" align="center" label="距离过期时间"></el-table-column>
      <el-table-column prop="placeIssue" header-align="center" align="center" label="签发地"></el-table-column>
      <el-table-column prop="certificateState" header-align="center" align="center" label="状态"></el-table-column>
      <el-table-column prop="certificateRemarks" header-align="center" align="center" label="备注"></el-table-column>
      <el-table-column fixed="right" header-align="center" align="center" width="150" label="操作">
        <template slot-scope="scope">
          <el-button type="text" size="small" @click="addOrUpdateHandle(scope.row.certificateId)">修改</el-button>
          <el-button type="text" size="small" @click="deleteHandle(scope.row.certificateId)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      @size-change="sizeChangeHandle"
      @current-change="currentChangeHandle"
      :current-page="pageIndex"
      :page-sizes="[10, 20, 50, 100]"
      :page-size="pageSize"
      :total="totalPage"
      layout="total, sizes, prev, pager, next, jumper"
    ></el-pagination>
    <!-- 弹窗, 新增 / 修改 -->
    <add-or-update v-if="addOrUpdateVisible" ref="addOrUpdate" @refreshDataList="getDataList"></add-or-update>
  </div>
</template>

<script>
import AddOrUpdate from "./certificatemanagement-add-or-update";
export default {
  data() {
    return {
      optionsCertificateState: [
        {
          value: "有效",
          label: "有效"
        },
        {
          value: "过期",
          label: "过期"
        },
        {
          value: "作废",
          label: "作废"
        }
      ],
      dataForm: {
        certificate_id: ""
      },
      dataList: [],
      pageIndex: 1,
      pageSize: 10,
      totalPage: 0,
      dataListLoading: false,
      dataListSelections: [],
      addOrUpdateVisible: false
    };
  },
  components: {
    AddOrUpdate
  },
  activated() {
    if (this.$route.query.certificateid !== undefined) {
      this.getDataListHome();
    } else {
      this.getDataList();
    }
  },
  methods: {
    getDataListHome() {
      var _this = this
      this.$http({
        url: this.$http.adornUrl("/generator/certificatemanagement/linkCertificateExpected"),
        method: "get",
        params: this.$http.adornParams({
          certificateId: this.$route.query.certificateid
        })
      }).then(({ data }) => {
        if (data && data.code === 0) {
          data.page.list.forEach(function(value, index, array) {
            value.overDate =
              _this.GetNumberOfDays(value.expirationDate) > 0
                ? _this.GetNumberOfDays(value.expirationDate)
                : "已过期";
          });
          this.dataList = data.page.list;
          this.totalPage = data.page.totalCount;
        } else {
          this.dataList = [];
          this.totalPage = 0;
        }
      });
    },
    // 获取数据列表
    getDataList() {
      var _this = this;
      this.dataListLoading = true;
      this.$http({
        url: this.$http.adornUrl("/generator/certificatemanagement/list"),
        method: "get",
        params: this.$http.adornParams({
          page: this.pageIndex,
          limit: this.pageSize,
          params: this.dataForm.certificate_id,
          certificateName: this.dataForm.certificateName,
          certificateState: this.dataForm.certificateState
        })
      }).then(({ data }) => {
        if (data && data.code === 0) {
          data.page.list.forEach(function(value, index, array) {
            value.overDate =
              _this.GetNumberOfDays(value.expirationDate) > 0
                ? _this.GetNumberOfDays(value.expirationDate)
                : "已过期";
          });
          this.dataList = data.page.list;
          this.totalPage = data.page.totalCount;
        } else {
          this.dataList = [];
          this.totalPage = 0;
        }
        this.dataListLoading = false;
      });
    },
    GetNumberOfDays(date2) {
      //获得天数
      //date1：开始日期，date2结束日期
      var a1 = new Date();
      var a2 = Date.parse(new Date(date2));
      var day = parseInt((a2 - a1) / (1000 * 60 * 60 * 24) + 1); //核心：时间戳相减，然后除以天数
      return day;
    },
    // 每页数
    sizeChangeHandle(val) {
      this.pageSize = val;
      this.pageIndex = 1;
      this.getDataList();
    },
    // 当前页
    currentChangeHandle(val) {
      this.pageIndex = val;
      this.getDataList();
    },
    // 多选
    selectionChangeHandle(val) {
      this.dataListSelections = val;
    },
    // 新增 / 修改
    addOrUpdateHandle(id) {
      this.addOrUpdateVisible = true;
      this.$nextTick(() => {
        this.$refs.addOrUpdate.init(id);
      });
    },
    // 重置
    resetForm(formName) {
      this.$refs[formName].resetFields();
    },
    // 删除
    deleteHandle(id) {
      var ids = id
        ? [id]
        : this.dataListSelections.map(item => {
            return item.certificateId;
          });
      this.$confirm(
        `确定对[id=${ids.join(",")}]进行[${id ? "删除" : "批量删除"}]操作?`,
        "提示",
        {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }
      ).then(() => {
        this.$http({
          url: this.$http.adornUrl("/generator/certificatemanagement/delete"),
          method: "post",
          data: this.$http.adornData(ids, false)
        }).then(({ data }) => {
          if (data && data.code === 0) {
            this.$message({
              message: "操作成功",
              type: "success",
              duration: 1500,
              onClose: () => {
                this.getDataList();
              }
            });
          } else {
            this.$message.error(data.msg);
          }
        });
      });
    },
    tableRowClassName({ row, rowIndex }) {
      console.log(row.certificateState); //  row.overDate === "已过期"
      if (Number(row.overDate) < 180) {
        return "warning-row caveat-row package_" + row.id;
      } else if (Number(row.overDate) > 180 && Number(row.overDate) < 365) {
        return "warning-row caveat-row365 package_" + row.id;
      } else if (row.overDate === "已过期" && row.certificateState === '有效') {
        return "warning-row caveat-row package_" + row.id;
      }
      return "package_" + row.id;
    }
  }
};
</script>
<style>
.el-table .warning-row {
  background: #fafafa;
}
.el-table .warning-row.caveat-row {
  background: hsla(41, 87%, 69%, 0.4);
}
.el-table .warning-row.caveat-row365 {
  background: hsla(41, 87%, 69%, 0.1);
}
</style>